import express from "express";
import mysql from "mysql2/promise";

const router = express.Router();

router.post("/list", async (req, res) => {
  try {
    const { host, port, user, password, database } = req.body;
    const conn = await mysql.createConnection({ host, port, user, password, database });

    // 获取 diy_table 表中的数据
    const [tableData] = await conn.query(`SELECT * from diy_table where IsDeleted = 0`);
    // console.log('获取到的表数据:', tableData);
    // const [rows] = await conn.query("SHOW TABLES");
    await conn.end();
    // 取出表名（字段名是动态的）
    // const tableNames = rows.map((row) => Object.values(row)[0]);
    res.json({ success: true, tables: tableData });
  } catch (error) {
    res.json({ success: false, message: `获取表失败: ${error.message}` });
  }
});

export default router;
